<?php

namespace app\admin\model\litestore;

use think\Model;

class Tag extends Model
{
    // 表名
    protected $name = 'litestore_tag';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [

    ];

    public function shop_tags_id($shopid)
    {
        $ids = db('litestore_tag_mid')->where('lid',$shopid)->select();
        $tids = [];
        foreach ($ids as $value) {
            $tids[] = $value['tid'];
        }
        return $tids;
    }


    /**
     * @param $shopid
     * @param $tags
     * 新增所属标签
     */
    public function add_tags($shopid,$tags)
    {
        foreach ($tags as $value) {
            db('litestore_tag_mid')->insert(['lid'=>$shopid,'tid'=>$value]);
        }
    }

    /**
     * @param $shopid
     * @param $tags
     * 修改商户标签
     */
    public function edit_tags($shopid, $tags)
    {
        $ids = $this->shop_tags_id($shopid);

        foreach ($tags as $value) {
            if(!in_array($value,$ids) && $value != 0){
                db('litestore_tag_mid')->insert(['lid'=>$shopid,'tid'=>$value]);
            }
        }

        foreach ($ids as $value) {
            if(!in_array($value,$tags)){
                db('litestore_tag_mid')->where(['lid'=>$shopid,'tid'=>$value])->delete();
            }
        }


    }
    







}
